<template>
  <div ref="echartDom"></div>
</template>

<script setup>
import {ref, defineProps, onMounted, onUnmounted, inject, shallowRef, defineExpose} from 'vue'
import {pxToRem} from "@/utils/index";

const props = defineProps({
  lineData: {
    type: Object,
    default: () => {
    }
  },
  dialogData: {
    type: Object,
    default: () => ({
      fontSizeTimes: 1
    })
  }
});

const echartDom = ref(null);
const myChart = shallowRef(null);
const echarts = inject('echarts');

const initEchart = () => {
  if (echartDom.value) {
    myChart.value = echarts.init(echartDom.value);
    let option = {
      tooltip: {
        trigger: 'axis',
        valueFormatter: (val) => `${val}${props?.lineData?.yAxisName}`
      },
      grid: {
        left: '2%',
        right: '7%',
        bottom: 0,
        top: `${props?.dialogData?.fontSizeTimes === 1 ? 25 : 10}%`,
        containLabel: true
      },
      dataZoom: { // 数据过多拖动逻辑
        type: "slider",
        startValue: 0,
        height: pxToRem(20),
        bottom: 0
      },
      xAxis: {
        min: 0,
        type: 'category',
        boundaryGap: false,
        data: props?.lineData?.xAxisData,
        axisLabel: {
          fontSize: pxToRem(9 * props?.dialogData?.fontSizeTimes),
          color: 'white',
          fontWeight: 'lighter',
        },
        axisLine: {
          show: true // 不显示坐标轴线
        }
      },
      yAxis: {
        name: props?.lineData?.yAxisName,
        min: 0,
        max: (value) => value.max || props?.lineData?.defaultYAxisMax,
        nameTextStyle: {
          color: "white",
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          fontWeight: 'lighter',
          padding: [0, 0, 0, pxToRem(-20 * props?.dialogData?.fontSizeTimes)]
        },
        nameGap: pxToRem(props?.dialogData?.fontSizeTimes === 1 ? 15 : 25),
        textStyle: {
          fontSize: pxToRem(9 * props?.dialogData?.fontSizeTimes)
        },
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: true, // 不显示坐标轴线
        },
        splitLine: {
          show: false,
        },
        axisLabel: {
          fontSize: pxToRem(9 * props?.dialogData?.fontSizeTimes),
          color: 'white',
          fontWeight: 'lighter'
        },
        ...props?.lineData?.yAxisConfig
      },
      series: [
        {
          data: props?.lineData?.seriesData,
          type: 'line',
          smooth: false,
          showSymbol: false,
          ...props?.lineData?.seriesConfig
        }
      ]
    }
    option && myChart.value.setOption(option, true);
  }
}

const updateChart = () => {
  if (myChart.value) {
    myChart.value.dispose(); // 销毁旧图表实例
  }
  initEchart();
};


defineExpose({
  updateChart
})

onMounted(() => {
  initEchart();
  window.addEventListener("resize", updateChart);
})

onUnmounted(() => {
  window.removeEventListener("resize", updateChart);
})
</script>

<style scoped lang="less">
.h150 {
  height: 150px;
}
.dialog-h150 {
  width: 1120px;
  height: 500px;
}
.h170 {
  height: 170px;
}
.dialog-h170 {
  width: 1120px;
  height: 500px;
}
</style>